package org.akomantoso.core.test;

import java.io.IOException;

import org.akomantoso.core.data.AnyOtherTypeElement;
import org.akomantoso.core.io.Externalizable;
import org.akomantoso.core.io.Input;
import org.akomantoso.core.io.InputConstants;
import org.akomantoso.core.io.InputFactory;
import org.akomantoso.core.io.Output;
import org.akomantoso.core.io.util.Instantiator;
import org.junit.Test;

public class ActKenya {
	
	public static final InputFactory INPUT_FACTORY = InputFactory.newFactory();
	
	@Test
	public void testRead20031210() throws Exception {
		TestUtil.readAndValidate("/xml/Act_Kenya_2003-12-10-short.xml");
	}
	
	
	@Test
	public void testRead19970822() throws Exception {
		TestUtil.readAndValidate("/xml/Act_Kenya_1997-08-22_3@2003-12-19.xml");
	}
	
	@Test
	public void testRead19891206() throws Exception {
		TestUtil.readAndValidate("/xml/Act_Kenya_1989-12-06.xml");
	}
	
	
	@Test
	public void testRead19800101_19891215() throws Exception {
		
		final InputFactory inputFactory = InputFactory.newFactory();
		
		inputFactory.addTag("http://www.my.com/2.0", "tag", new Instantiator<AnyOtherTypeElement>() {
			@Override
			public Tag instantiate() {
				return new Tag();
			}
		});
		inputFactory.addTag("http://www.myTags.com/2.0", ProprietaryExt.ELEMENT, new Instantiator<AnyOtherTypeElement>() {
			@Override
			public ProprietaryExt instantiate() {
				return new ProprietaryExt();
			}
		});
		TestUtil.readAndValidate("/xml/Act_Kenya_1980-01-01@1989-12-15.xml",inputFactory);
	}
	
	
	
	public static class MyWorkFlowTags implements Externalizable {
		public static final String ELEMENT = "myWorkFlowTags";
		public <T extends Output> void writeExternal(T out) throws IOException {
			out.writeStart(ELEMENT);
			out.writeEnd();
		}
		public <T extends Input> void readExternal(T in) throws IOException {
		}
	}
	
	public static class ProprietaryExt implements AnyOtherTypeElement {
		public static final String ELEMENT = "proprietary";
		private MyWorkFlowTags _tags;
		private String _source;
		
		public <T extends Input> void readExternal(T in) throws IOException {
			_source = in.readAttribute("source");
			in.readNextStartOrEndElement();
			if (MyWorkFlowTags.ELEMENT == in.getLocalName()) {
				_tags = new MyWorkFlowTags();
				_tags.readExternal(in);
				in.readNextStartOrEndElement();
			}
		}

		@Override
		public <T extends Output> void writeExternal(T out) throws IOException {
			out.writeStart("myTags:" + ELEMENT);
			out.writeAttribute("source", _source);
			_tags.writeExternal(out);
			out.writeEnd();
		}
		
		
	}
	
	
	public static class Tag implements AnyOtherTypeElement {
		private String _value = null;
		@Override
		public <T extends Output> void writeExternal(T out) throws IOException {
			out.writeStart("my:tag");
			out.writeRaw(_value);
			out.writeEnd();
		}

		@Override
		public <T extends Input> void readExternal(T in) throws IOException {
			int eventType;
			while (true) {
				eventType = in.readNext();
				if (eventType == InputConstants.CHARACTERS) {
					if (_value == null) {
						_value = in.readContent().trim();
					} else {
						_value = _value + in.readContent().trim();
					}
					continue;
				}
				if (eventType == InputConstants.END_ELEMENT && "tag".endsWith(in.getLocalName())) {
					break;
				}
			}
		}

	}
	
	
}